package JasPerl::Compiler::Util;
use base qw{ Exporter };

use strict;
use warnings;

use URI;

our @EXPORT_OK = qw{ abs_path escape quote };

my %ESCAPE = (
    "\a" => '\\a',
    "\f" => '\\f',
    "\n" => '\\n',
    "\r" => '\\r',
    "\t" => '\\t'
);

sub abs_path {
    my ($path, $base) = @_;
    return URI->new_abs($path, $base)->path;
}

sub quote {
    my $s = shift;
    $s =~ s/([\\\'])/\\$1/g;
    return  "'" . $s . "'";
}

sub escape {
    my $s = shift;
    $s =~ s/([[:cntrl:]])/$ESCAPE{$1} || sprintf('\\x%02x', ord($1))/egs;
    return '"' . $s . '"';
}

1;

__END__
